package org.realogic.cfm.common.md5;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Encryption {
	private Encryption() {

	}

	public static String encrypt(String password) {
		String digesta_last = null;
		try {
			MessageDigest alga = MessageDigest.getInstance("SHA-1");
			alga.update(password.getBytes());
			byte[] digesta = alga.digest();
			digesta_last = byte2hex(digesta);
		} catch (NoSuchAlgorithmException ex) {
			throw new IllegalStateException("encrypt error........");
		}
		return digesta_last;
	}

	private static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
			if (n < b.length - 1)
				hs = hs.concat(":");
		}
		return hs.toUpperCase();
	}

	public static String getMD5Str(String str) {
		MessageDigest messageDigest = null;

		try {
			messageDigest = MessageDigest.getInstance("MD5");

			messageDigest.reset();

			messageDigest.update(str.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			System.out.println("NoSuchAlgorithmException caught!");
			System.exit(-1);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		byte[] byteArray = messageDigest.digest();

		StringBuffer md5StrBuff = new StringBuffer();

		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
				md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
			else
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
		}

		return md5StrBuff.toString();
	}

	public static void main(String args[]) {
		
		//E10ADC3949BA59ABBE56E057F20F883E
		//E10ADC3949BA59ABBE56E057F20F883E

		System.out.println(getMD5Str("admin").toUpperCase());
		//GID == zoneid << 32 + gid
		long x = 21474903529322l;
		long y = 5000l<<32;
		//67049322 
		//67049322
		System.out.println(x-y);
		
	}
}